#!/usr/bin/perl -w

#=======
#cron:
#10 23 * * * /home/jctong/scripts/drive_status/insert_drive_status.pl

use strict;
use DBI;

use Chart::Strip;

        my $db="jctong";
        my $table="drive_status";
        my $host="ilwrath";
        my $user="nbuadmin";
        my $password="asdfjk";

       my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host",$user,$password) or die "Can't connect to database: $DBI::errstr
\n";


my @color=("FFCC00","00CC00","FF0000","0033FF","CCCC33","993399","990000","66FF33","FF9900","3399CC");

foreach my $drive (0..9){

	my $drivelabel="drive_".$drive;
my $ch = Chart::Strip->new(
             title   => $drivelabel.' Last 24 Hours',
             # other options ...
         );

	my $onedayago=int(time) - 86400;
	my $query="select timestamp,status from drive_status where drive_index=$drive AND timestamp > $onedayago order by timestamp";
	my $data=&formatdata($query);

	$ch->add_data($data,{style=>'box',color=>pop @color,label=>$drivelabel,filled=>1});
	open(OUTPUT,">24hrs_drive_$drive.png");
	print OUTPUT $ch->png;
}


@color=("FFCC00","00CC00","FF0000","0033FF","CCCC33","993399","990000","66FF33","FF9900","3399CC");
#this section to show data from the past two weeks
foreach my $drive (0..9){

	my $drivelabel="drive_".$drive;
my $ch = Chart::Strip->new(
             title   => $drivelabel.' Last 2 weeks',
             # other options ...
         );

	my $twoweeksago=int(time) - 1209600;
	my $query="select timestamp,status from drive_status where drive_index=$drive AND timestamp > $twoweeksago order by timestamp";
	my $data=&formatdata($query);

	$ch->add_data($data,{style=>'box',color=>pop @color,label=>$drivelabel,filled=>1});
	open(OUTPUT,">2weeks_drive_$drive.png");
	print OUTPUT $ch->png;
}



#this section is to print out all drive status on a single graph.
#@color=("FFCC00","00CC00","FF0000","0033FF","CCCC33","993399","990000","66FF33","FF9900","3399CC");
#my $ch = Chart::Strip->new(
#             title   => 'All Drive Status',
#             # other options ...
#         );
#
#foreach my $drive (0..9){
#	my $drivelabel="drive_".$drive;
#	my $query="select timestamp,status from drive_status where drive_index=$drive order by timestamp";
#	my $data=&formatdata($query);
#
#	$ch->add_data($data,{style=>'line',color=>pop @color,label=>$drivelabel});
#}
#
#open(OUTPUT,">all.png");
#print OUTPUT $ch->png;
#







#http://sedition.com/perl/variegated.html
sub random_hex_color {
# only allowed values are 3 and 6, 6 is default
    my $size = 6;
    my @hex = ( 0 .. 9, 'a' .. 'f' );
    my @color;
    push @color, @hex[rand(@hex)] for 1 .. $size;
    return join('','', @color);
}


sub formatdata{

#input: a sql statement that returns a 2-dimensional array like this:
#+------------+-------+
#| unixtime   | Kills |
#+------------+-------+
#| 1214691944 | 5     | 
#| 1214723014 | 1     | 
#| 1214723837 | 5     | 
#+------------+-------+
#3 rows in set (0.00 sec)
	
	my $query=$_[0];
	my $sth=$dbh->prepare($query);
	$sth->execute();
	my @result;
	while(my($unixtime,$kills)=$sth->fetchrow_array()){
		push(@result,{time=>$unixtime,value=>$kills});
	}
	return \@result;
}
